package sparrowGui.data
{
	import sparrowGui.i.ITreeNode;

	/**
	 * 树形控件组合
	 * @author Pelephone
	 * @website http://cnblogs.com/pelephone
	 */
	public class TreeComposite
	{
		private var _id:int;
		
		private var _folded:Boolean = false;
		private var _selected:Boolean = false;
		
		private var _parent:TreeComposite;
		private var _children:Vector.<TreeComposite>;
		private var _data:ITreeNode;
		/**
		 * 树形节点组件组合数据
		 */
		public function TreeComposite(nodeId:int)
		{
			_children = new Vector.<TreeComposite>();
			id = nodeId;
		}
		
		public function add(node:TreeComposite):void
		{
			node.parent = this;
			_children.push(node);
		}
		
		/**
		 * 通过索引获取其中一项
		 * @param index
		 * @return 
		 */
		public function getChildById(index:int):TreeComposite
		{
			if ((index >= 0) && (index < _children.length)) {
				return _children[index];
			} else {
				return null;
			}
		}
		
		/**
		 * 子项长度
		 * @return 
		 */
		public function get numChildren():int 
		{
			return _children.length;
		}
		
		public function remove(c:TreeComposite):void
		{
			if (c === this) {
				// 移出所有子节点
				for (var i:int = 0; i < _children.length; i++) {
					safeRemove(_children[i]); // 移出子节点
				}
				this._children = new Vector.<TreeComposite>(); // 移出子项的引用
				this.removeParentRef(); // 断开父类的引用
//				this._childrenNameHm = {};
			} else {
				for (var j:int = 0; j < _children.length; j++) {
					if (_children[j] == c) {
						safeRemove(_children[j]); // 移出子节点
						_children.splice(j, 1); // 移出引用
					}
				}
			}
		}
		
		/**
		 * 移除父节点
		 */
		private function removeParentRef():void { 
			this.parent = null;
		}
		
		/**
		 * 安全移除
		 * @param c
		 */
		private function safeRemove(c:TreeComposite):void
		{
			if (c) {
				c.remove(c);
			} else {
				c.removeParentRef();
			}
		}

		public function set data(value:ITreeNode):void
		{
			_data = value;
		}
		
		/**
		 * 节点的vo数据
		 * @return 
		 */
		public function get data():ITreeNode
		{
			return _data;
		}

		/**
		 * 唯一id
		 */
		public function get id():int
		{
			return _id;
		}

		/**
		 * @private
		 */
		public function set id(value:int):void
		{
			_id = value;
		}

		/**
		 * 是否选中
		 */
		public function get selected():Boolean
		{
			return _selected;
		}

		/**
		 * @private
		 */
		public function set selected(value:Boolean):void
		{
			_selected = value;
		}

		/**
		 * 是否合起
		 */
		public function get folded():Boolean
		{
			return _folded;
		}

		/**
		 * @private
		 */
		public function set folded(value:Boolean):void
		{
			_folded = value;
		}
		
		/**
		 * 相对于根的深度
		 * @return 
		 */
		public function get depth():int
		{
			return getDepth(this);
		}
		
		/**
		 * 递归计算出该组合的深度
		 */
		private function getDepth(c:TreeComposite,dep:int=0):int
		{
			if(c.parent!=null) return getDepth(c.parent,(dep+1));
			else return dep;
		}

		public function get parent():TreeComposite
		{
			return _parent;
		}

		public function set parent(value:TreeComposite):void
		{
			_parent = value;
		}

	}
}